/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dale2507.gates.gate;

import dale2507.gates.GatePlugin;
import dale2507.gates.Messenger;
import dale2507.gates.data.Settings;
import dale2507.gates.data.permissions.Permissions;
import dale2507.gates.events.GateStatusChangeEvent;
import dale2507.gates.events.GateTravelEvent;
import dale2507.gates.exceptions.PermissionException;
import dale2507.gates.misc.BlockHelper;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.entity.Vehicle;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockRedstoneEvent;

/**
 *
 * @author Dale
 */
public abstract class IIris implements Listener {
    
    private Gate gate;
    private Block irisPower;
    private boolean engaged;
    
    IIris(Gate gate, Block irisPower) {
        this.gate = gate;
        this.irisPower = irisPower;
        
        this.setEngaged(false);
        
        Bukkit.getPluginManager().registerEvents(this, GatePlugin.getInstance());
    }

    protected Gate getGate() {
        return gate;
    }
    
    private boolean hasPower() {
        return irisPower.isBlockPowered() || irisPower.isBlockIndirectlyPowered();
    }

    public boolean isEngaged() {
        return engaged;
    }

    public final void setEngaged(boolean engaged) {
        this.engaged = engaged;
        
        Material m = Material.AIR;
        if (this.engaged) {
            m = Settings.getInstance().connection.irisMaterial();
        } else if (this.getGate().getStatus() == Gate.Status.INCOMING) {
            m = Material.STATIONARY_WATER;
        }
        
        for (Block b : this.gate.getRing().getInnerBlocks()) {
            b.setType(m);
        }
    }
    
    private void setIrisFromStatus(Gate.Status status) {
        if (status == Gate.Status.INCOMING) {
            this.setEngaged(true);
        } else if (status == Gate.Status.INACTIVE || 
                status == Gate.Status.BROKEN) {
            this.setEngaged(false);
        }
    }
    
    @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
    public void onGateStatusChange(GateStatusChangeEvent event) {
        if (event.getGate().equals(this.gate)) {
            if (this.hasPower()) {
                this.setIrisFromStatus(event.getNewStatus());
            } else if (event.getNewStatus() == Gate.Status.INACTIVE && event.getOldStatus() == Gate.Status.DISABLED) {
                this.setEngaged(false);
            }
        }
    }
    
    @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
    public void onIrisPowerChanged(BlockRedstoneEvent event) {
        if (BlockHelper.isNeighbour(event.getBlock(), this.irisPower)) {
            if (event.getNewCurrent() > 0) {
                this.setIrisFromStatus(this.getGate().getStatus());
            } else {
                this.setEngaged(false);
            }
        }
    }
    
    @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
    public void onIrisBroken(BlockBreakEvent event) {
        if (this.isEngaged()) {
            for (Block b : this.gate.getRing().getInnerBlocks()) {
                if (b.equals(event.getBlock())) {
                    event.setCancelled(true);
                    if (event.getPlayer() != null) Messenger.sendMessage(event.getPlayer(), Messenger.ERROR, "You cannot destroy an iris");
                }
            }
        }
    }
    
    @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
    public void onTraveller(GateTravelEvent event) {
        if (event.getDestination().equals(this.getGate()) && this.isEngaged()) {
            Player p = null;
            if (event.getEntity() instanceof Player) p = (Player) event.getEntity();
            if (event.getEntity() instanceof Vehicle) {
                Vehicle v = (Vehicle) event.getEntity();
                if (v.getPassenger() != null && v.getPassenger() instanceof Player)
                    p = (Player) event.getEntity();
            }
            
            if (p != null) {
                try {
                    if (Permissions.getPermissions().getBypassPermissions().bypassIris(p))
                        this.setEngaged(false);
                } catch (PermissionException ex) {
                    Logger.getLogger(IIris.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
}
